package yz.cp.back.boot;

import com.google.common.collect.Lists;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.*;

public class YamlConfig {
    public static String PROFILES = "dev";
    private static final YamlConfig CONFIG = new YamlConfig(new String[]{"/application-dev.yml"});
    private Yaml yaml = new Yaml();
    private Map<String, Object> root = new HashMap();

    private YamlConfig(String... fileNames) {
        List list = Lists.newArrayList(fileNames);
        Map<String, Object> map = new HashMap(0);
        for (Object o : list) {
            Map<String, Object> conf = this.getRoot(o.toString());
            map = this.marge((Map) map, conf);
        }
        this.root.putAll(map);
        this.iteratorYml(map, null);
    }

    public void iteratorYml(Map map, String key) {
        Iterator iterator = map.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            Object key2 = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof LinkedHashMap) {
                if (key == null) {
                    this.iteratorYml((Map) value, key2.toString());
                } else {
                    this.iteratorYml((Map) value, key + "." + key2.toString());
                }
            }else {
                if (key == null) {
                    this.root.put(key2.toString(), value.toString());
                }

                if (key != null) {
                    if(value instanceof String){
                        this.root.put(key + "." + key2.toString(), value.toString());
                    }
                    if(value instanceof Integer)
                    {
                        this.root.put(key + "." + key2.toString(), value);
                    }
                }
            }
            }
    }

    public static YamlConfig getConfig() {
        return CONFIG;
    }

    private Object getValue(String key) {
        Object value = this.root.get(key);
        if (value == null) {
            throw new RuntimeException(String.format("The value corresponding to the key is not set.key[%s]", key));
        } else {
            return value;
        }
    }

    public String get(String key) {
        Object value = this.getValue(key);
        return value.toString();
    }

    public Object getOj(String key) {
        Object value = this.getValue(key);
        return value;
    }

    private Map<String, Object> getRoot(String resource) {
        InputStream in;
        try {
            in = YamlConfig.class.getResourceAsStream(resource);
            return (Map) this.yaml.load(in);
        } catch (Exception var4) {
            throw new RuntimeException("It failed in reading the configuration file.", var4);
        }
    }

    private Map<String, Object> marge(Map<String, Object> map1, Map<String, Object> map2) {
        Map<String, Object> merged = new HashMap(map1);
        Iterator var4 = map2.keySet().iterator();

        while (var4.hasNext()) {
            String key = (String) var4.next();
            if (merged.containsKey(key)) {
                throw new RuntimeException(String.format("The key overlaps. [%s]", key));
            }

            merged.put(key, map2.get(key));
        }

        return merged;
    }

    public static void setPROFILES(String PROFILES) {
        YamlConfig.PROFILES = PROFILES;
    }

    public static String getPROFILES() {
        return PROFILES;
    }
}
